package com.pro.controller;

import com.pro.pojo.JsonData;
import com.pro.service.JsUserHandService;
import com.pro.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/userhand")
public class JsUserHandController {
    private String acatar_url;

    @Autowired
    private JsUserHandService service;
    @Autowired
    private FastDFSClientUtil dfsClient;
    // 上传文件
    private static int i = 0;

    String resAccess =null;
    @RequestMapping(value = "/upFile", method = RequestMethod.POST)
    @ResponseBody
    public JsonData upload(MultipartFile[] files, HttpSession session)
            throws IllegalStateException, IOException {
        String resAccessUrl =null;
        try {
            for (MultipartFile file : files) {
                String fileUrl = dfsClient.uploadFile(file);
                //路径
                resAccessUrl = dfsClient.getResAccessUrl(fileUrl);
                resAccess=resAccessUrl;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("path",resAccessUrl);
        return map!=null?JsonData.buildSuccess(map):JsonData.buildError("服务端错误");
    }

    @RequestMapping("/updatehead")
    @ResponseBody
    public JsonData updatehead(String account){
        Boolean flag=service.updatehead(account,resAccess);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }

    @RequestMapping("/getUrl")
    @ResponseBody
    public JsonData gethead(String account){
        String flag=service.getUrl(account);
        return flag!=null?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }
}
